package agents;

import extra.Position;
import game.constants.ReferenceAgent;
import game.constants.ReferenceObject;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.Agent;
import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionModifySpetialFeature;
import constants.Constants;

public class AgentAttackMonster extends Agent{

	SpetialFeature objectIdToAttack;
	SpetialFeature objectIdToFollow;
	SpetialFeature visionRagne;
	
	ActionModifySpetialFeature actionModifyTargetToAttack;
	ActionModifySpetialFeature actionModifyTargetToFollow;
	
	int cont =0;

	@Override
	public void initializeActionList() {
		actionModifyTargetToAttack= new ActionModifySpetialFeature();
		listActions.add(actionModifyTargetToAttack);
		
		actionModifyTargetToFollow= new ActionModifySpetialFeature();
		listActions.add(actionModifyTargetToFollow);

	}

	@Override
	public void initializeSpecialFeatures() {
		
		objectIdToAttack= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_ATTACK);
		addSpetialFeature(objectIdToAttack);
		
		visionRagne = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VISION_RANGE);
		addSpetialFeature(visionRagne);
		
	}

	@Override
	public void initializeName() {
		name="Agent Search Attack Object";
		
	}

	@Override
	public void initializeId() {
		this.id=ReferenceAgent.AGENT_SEARCH_TARGET_TO_ATTACK;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {

		
		for(WorldObject objToAttack : universe.getListObjects()){
			
			if(objToAttack.getObjectReference()!=ReferenceObject.OBJECT_REFERENCE_PLAYER){
				continue;
			}
			double distance = Position.DistanceBetween(object.getPosition(), objToAttack.getPosition());
			
			if(distance<visionRagne.getValue()){
				
				actionModifyTargetToFollow.setSource(object);
				actionModifyTargetToFollow.setTarget(object);
				actionModifyTargetToFollow.setFeatureId(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_FOLLOW);
				actionModifyTargetToFollow.setValue(objToAttack.getID());
				universe.setAction(actionModifyTargetToFollow);
		
				actionModifyTargetToAttack.setSource(object);
				actionModifyTargetToAttack.setTarget(object);
				actionModifyTargetToAttack.setFeatureId(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_ATTACK);
				actionModifyTargetToAttack.setValue(objToAttack.getID());				
				universe.setAction(actionModifyTargetToAttack);
				return;
			}
		}
		actionModifyTargetToFollow.setSource(object);
		actionModifyTargetToFollow.setTarget(object);
		actionModifyTargetToFollow.setFeatureId(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_FOLLOW);
		actionModifyTargetToFollow.setValue(Constants.NO_OBJECT_ID);
		universe.setAction(actionModifyTargetToFollow);

		actionModifyTargetToAttack.setSource(object);
		actionModifyTargetToAttack.setTarget(object);
		actionModifyTargetToAttack.setFeatureId(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_ATTACK);
		actionModifyTargetToAttack.setValue(Constants.NO_OBJECT_ID);				
		universe.setAction(actionModifyTargetToAttack);
	}

}	
	
	
	/*ActionModifySpetialFeature modifyIdObjectToAttack;
	
	SpetialFeature  objectIdToAttack;
	SpetialFeature visionRange;
	
	@Override
	public void initializeActionList() {
		modifyIdObjectToAttack= new ActionModifySpetialFeature();
		addAction(modifyIdObjectToAttack);
		
		
	}

	@Override
	public void initializeSpecialFeatures() {
		objectIdToAttack=new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_ATTACK);
		addSpetialFeature(objectIdToAttack);
		
		visionRange=new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VISION_RANGE);
		addSpetialFeature(visionRange);
	}

	@Override
	public void initializeName() {
		name="Agent Attack Monster";
		
	}

	@Override
	public void initializeId() {
		this.id=ReferenceAgent.AGENT_ATTACK_MONSTER;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		ArrayList<WorldObject> listPlayer = universe.getListObjects(ReferenceObject.OBJECT_REFERENCE_PLAYER);
		if(listPlayer.isEmpty()){
			return;
		}
		double distance=-1;
		for(WorldObject obj : listPlayer){
			distance= Position.DistanceBetween(obj.getPosition(), object.getPosition());
			if(distance<visionRange.getValue()){
				
				modifyIdObjectToAttack.setSource(object);
				modifyIdObjectToAttack.setTarget(object);
				modifyIdObjectToAttack.setFeatureId(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_ATTACK);
				modifyIdObjectToAttack.setValue(obj.getID());
				universe.setAction(modifyIdObjectToAttack);
				
				modifyIdObjectToAttack.setSource(object);
				modifyIdObjectToAttack.setTarget(object);
				modifyIdObjectToAttack.setFeatureId(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_FOLLOW);
				modifyIdObjectToAttack.setValue(Constants.NO_OBJECT_ID);
				universe.setAction(modifyIdObjectToAttack);
				
				return;
			}
		}
	
		
	}
	*/
	

//}
